# 类定义-----登录附属数据处理

# 导入模块
import rsa
import base64


# RSA密钥类
class RSAMethod():
    def __init__(self):
        pass

    def Pub_strToKey(self, string):
        # 字符串编码
        byte_str = base64.b64decode(string)

        if (len(byte_str) < 162):
            # 不合法的密钥
            return False

        hex_str = ""

        # 按位转换16进制
        for i in byte_str:
            h = hex(i)[2:]
            h = h.rjust(2, '0')
            hex_str += h

        # 取模 取指
        m_start = 29 * 2
        e_start = 159 * 2
        m_len = 128 * 2
        e_len = 3 * 2

        modulus = hex_str[m_start:m_start + m_len]
        exponent = hex_str[e_start:e_start + e_len]

        modulus = int(modulus, 16)
        exponent = int(exponent, 16)

        pubKey = rsa.PublicKey(modulus, exponent)
        # print(modulus)
        # print(exponent)
        return pubKey


    def Pri_strToKey(self, string):
        # 字符串编码
        byte_str = base64.b64decode(string)

        if (len(byte_str) < 607):
            # 不合法的密钥
            return False

        hex_str = ""

        # 按位转换16进制
        for i in byte_str:
            h = hex(i)[2:]
            h = h.rjust(2, '0')
            hex_str += h

        print(hex_str)
        # 取模 取指
        m_start = 29 * 2
        e_start = 159 * 2
        m_len = 128 * 2
        e_len = 3 * 2

        modulus = hex_str[m_start:m_start + m_len]
        exponent = hex_str[e_start:e_start + e_len]

        modulus = int(modulus, 16)
        exponent = int(exponent, 16)

        priKey = rsa.PrivateKey()
        return priKey



if __name__ == "__main__":
    a = RSAMethod()
    a.Pub_strToKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2EnR9nsvvWJjmfsZH4olSpSO4wAzuGA5ALg+t9ewoWaqI14j8Sw7WA/mSUZhN/8MJJ294mGeCArTitcCOFP9plTxBDsjRXaESNfXuxrHLWrcL7C1uiFCZXt5FMBRDXKrYj4AKKzzw+DcgJ5HaHruC8QTRYxK8bQkVoE5GMFGwQwIDAQAB")
    a.Pri_strToKey("MIICWwIBAAKBgQC2EnR9nsvvWJjmfsZH4olSpSO4wAzuGA5ALg+t9ewoWaqI14j8Sw7WA/mSUZhN/8MJJ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